package cn.psvmc.cxzapi.controller.eqb;

import cn.psvmc.cxzapi.model.eqb.TemplateDTO;
import cn.psvmc.cxzapi.service.eqb.EqbService;
import cn.psvmc.cxzapi.util.ToolUtils;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * @author ywx
 * @className EqbController
 * @description e签宝
 * @date 2024/9/29 21:53
 **/
@RestController
@RequestMapping("/eqb")
@Slf4j
public class EqbController {
    @Resource
    private EqbService eqbService;

    /**
     * @Description 获取模板文件上传地址
     * @Date 2024/10/3 20:10
     * @Author YWX
     * @Param [object]
     * @Return java.lang.String
     **/
    @PostMapping("/docTemplates/createByUploadUrl")
    public String createByUploadUrl(@RequestBody JSONObject dto) {
        ToolUtils.validation(new Object[]{
                dto.getString("contentMd5"), "模板文件md5值", 2
                , dto.getString("contentType"), "目标文件的MIME类型", 2
                , dto.getString("fileName"), "文件名称", 2
                , dto.getString("fileSize"), "文件大小", 2
        });
        return eqbService.createByUploadUrl(dto);
    }

    /**
     * @Description 查询模板文件详情
     * @Date 2024/10/3 20:10
     * @Author YWX
     * @Param [templateId]
     * @Return java.lang.String
     **/
    @GetMapping("/docTemplates/{templateId}")
    public String getDocTemplate(@PathVariable("templateId") String templateId) {
        ToolUtils.validation(new Object[]{
                templateId, "模板编号", 2
        });
        return eqbService.getDocTemplate(templateId);
    }

    /**
     * @Description 发送GET请求
     * @Date 2024/10/15 11:42
     * @Author YWX
     * @Param [api]
     * @Return java.lang.String
     **/
    @GetMapping("/sendGet/{api}")
    public String sendGet(@PathVariable("api") String api) {
        ToolUtils.validation(new Object[]{
                api, "接口地址", 2
        });
        api = "/" + api.replaceAll("_", "/");
        return eqbService.testGet(api);
    }

    /**
     * @Description 发生POST请求
     * @Date 2024/10/15 17:20
     * @Author YWX
     * @Param [reqBodyObj, api]
     * @Return java.lang.String
     **/
    @PostMapping("/sendPost/{api}")
    public String sendPost(@RequestBody JSONObject reqBodyObj, @PathVariable("api") String api) {
        ToolUtils.validation(new Object[]{
                api, "接口地址", 2
        });
        api = "/" + api.replaceAll("_", "/");
        return eqbService.testPost(reqBodyObj, api);
    }

    /**
     * @Description 填充内容生成PDF
     * @Date 2024/10/3 20:30
     * @Author YWX
     * @Param [dto]
     * @Return java.lang.String
     **/
    @PostMapping("/files/createByTemplate")
    public String createByTemplate(@RequestBody TemplateDTO dto) {
        ToolUtils.validation(new Object[]{
                dto.getFileName(), "文件名称", 2
                , dto.getDocTemplateId(), "模板编号", 2
                , dto.getComponents(), "填充内容", 3
        });
        return eqbService.createByTemplate(dto);
    }

    @PutMapping("/{fuploadUrl1}")
    public String fileUpload(@PathVariable("fuploadUrl1") String fuploadUrl1, @RequestParam("file") MultipartFile file, HttpServletRequest request) {
        return eqbService.fileUpload(fuploadUrl1, file, request);
    }
}
